#include "tcpServer.hpp"
#include "dictServer.hpp"
#include "Command.hpp"

std::string tcptest(const std::string& str, const InetAddr& aim)
{
    return "success";
}

void tcptest2(int sockfd, const InetAddr& aim)
{
    while (true)
    {
        char buf[128];
        
        int num = read(sockfd, buf, sizeof(buf));
        if (num > 0)
        {
            buf[num] = 0;

            LOG(LogLevel::DEBUG) << aim.getStringAddr() << " say#" << buf;

            std::string back = "echo#";
            back += buf;
            write(sockfd, back.c_str(), back.size());
        }
        else if (num < 0)
        {
            LOG(LogLevel::DEBUG) << "对话终止...";
            close(sockfd);
            break;
        }
        else
        {
            LOG(LogLevel::DEBUG) << "对话结束...";
            close(sockfd);
            break;
        }
    }
}

void tcptest3(int sockfd, const InetAddr& aim)
{
    DictServer dict;
    dict.getWords();

    while (true)
    {
        char buf[128];
        
        int num = read(sockfd, buf, sizeof(buf));
        if (num > 0)
        {
            buf[num] = 0;

            std::string word = buf;
            

            std::string back = dict.translate(word, aim);
            back += '\n';
            
            write(sockfd, back.c_str(), back.size());
        }
        else if (num < 0)
        {
            LOG(LogLevel::DEBUG) << "对话终止...";
            close(sockfd);
            break;
        }
        else
        {
            LOG(LogLevel::DEBUG) << "对话结束...";
            close(sockfd);
            break;
        }
    }
}

void tcptest4(int sockfd, const InetAddr& aim)
{
    Command com;
    FILE* fp = nullptr;

    while (true)
    {
        char buf[128];
        
        int num = read(sockfd, buf, sizeof(buf));
        if (num > 0)
        {
            buf[num] = 0;

            std::string word = buf;
            std::string back;
            
            if (com.findCommand(word) == true)
            {
                fp = popen(word.c_str(), "r");
            }
            else
            {
                LOG(LogLevel::WARNING) << "command not find:" << word;
                back = "not find command";
            }
            if (fp == nullptr)
            {
                LOG(LogLevel::WARNING) << "popen failure";
                back = "not find command";
            }

            char bufCom[1024];

            while (fp != nullptr && fgets(bufCom, sizeof(bufCom) - 1, fp))
            {
                back += bufCom;
                // back += '\n';
            }

            pclose(fp);
            
            write(sockfd, back.c_str(), back.size());
        }
        else if (num < 0)
        {
            LOG(LogLevel::DEBUG) << "对话终止...";
            close(sockfd);
            break;
        }
        else
        {
            LOG(LogLevel::DEBUG) << "对话结束...";
            close(sockfd);
            break;
        }
    }
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "正确格式: ./tcpServer port" << std::endl;
        return 1;
    }

    int port = std::stoi(argv[1]);

    TcpServer test(port, tcptest4);

    test.init();
    test.run();

    return 0;
}
